using System;
using System.Collections.Generic;
using System.Data;
using FarPoint.Win.Spread;
using FarPoint.Win.Spread.Data;
using Spread.Common.DataStore;

namespace Spread.Table.Features
{
    public partial class TableStyles : Spread.Common.Features.Sample.BaseSampleControl
    {
        #region Fields
        private TableView _activeTable;
        private AccessDataSource _dataSource;
        #endregion

        #region ctor
        public TableStyles()
        {
            InitializeComponent();
            Name = "Table Styles";

            _dataSource = new DataService().AccessDataSource;

            SetSheetProperties(fpSpread.ActiveSheet);
            AddSampleTable(fpSpread.ActiveSheet);

            LoadComboBoxStyles();

            fpSpread.SelectionChanged += FpSpread_SelectionChanged;
            comboStyle.SelectedIndexChanged += ComboStyle_SelectedIndexChanged;

            chkHeaderRow.CheckedChanged += ChkHeaderRow_CheckedChanged;
            chkFirstColumn.CheckedChanged += ChkFirstColumn_CheckedChanged;
            chkTotalRow.CheckedChanged += ChkTotalRow_CheckedChanged;
            chkLastColumn.CheckedChanged += ChkLastColumn_CheckedChanged;
            chkBandedColumn.CheckedChanged += ChkBandedColumn_CheckedChanged;
            chkBandedRow.CheckedChanged += ChkBandedRow_CheckedChanged;
            chkFilterButton.CheckedChanged += ChkFilterButton_CheckedChanged;
        }
        #endregion

        #region Private methods
        private void SetSheetProperties(SheetView sheet)
        {
            fpSpread.StatusBarVisible = true;
            sheet.SetMultipleRowHeights(0, sheet.RowCount, 20);
            sheet.SetActiveCell(2, 1);
        }

        private void AddSampleTable(SheetView sheet)
        {
            DataTable dataTable = _dataSource.GetDataFromTable("Nwind.mdb", "Products").Tables[0];
            SpreadDataBindingAdapter adapter = new SpreadDataBindingAdapter();
            adapter.Spread = fpSpread;
            adapter.SheetName = "Sheet1";
            adapter.DataSource = dataTable;
            adapter.MapperInfo = new MapperInfo(2, 1, 30, dataTable.Columns.Count);
            adapter.FillSpreadDataByDataSource();

            int col = 1;
            foreach (DataColumn column in dataTable.Columns)
                sheet.Cells[1, col++].Text = (column.Caption);

            _activeTable = sheet.AddTable("Table1", 1, 1, 31, dataTable.Columns.Count,
                TableStyle.TableStyleLight1);

            for (int j = 1; j < col; ++j)
                sheet.Columns[j].Width = sheet.Columns[j].GetPreferredWidth();
        }

        private void LoadComboBoxStyles()
        {
            List<string> styles = fpSpread.TableStyleCollection.BuiltInTableStyleName;
            styles[0] = "None";
            comboStyle.DataSource = styles;

            foreach (var style in comboStyle.Items)
            {
                if (style.ToString() == _activeTable.StyleName)
                {
                    comboStyle.SelectedItem = style;
                    break;
                }
            }
        }
        #endregion

        #region Event handlers
        private void FpSpread_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            bool isTableSelected = fpSpread.ActiveSheet.GetTable(e.Range.Row, e.Range.Column) != null;
            _activeTable = fpSpread.ActiveSheet.GetTable(e.Range.Row, e.Range.Column);
            chkHeaderRow.Enabled = isTableSelected;
            chkFirstColumn.Enabled = isTableSelected;
            chkTotalRow.Enabled = isTableSelected;
            chkLastColumn.Enabled = isTableSelected;
            chkBandedColumn.Enabled = isTableSelected;
            chkBandedRow.Enabled = isTableSelected;
            chkFilterButton.Enabled = isTableSelected;
            comboStyle.Enabled = isTableSelected;
        }

        private void ComboStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            string styleName = comboStyle.SelectedItem.ToString();
            _activeTable.StyleName = styleName;
        }

        private void ChkFilterButton_CheckedChanged(object sender, EventArgs e)
        {
            if (_activeTable != null)
                _activeTable.FilterButtonVisible = chkFilterButton.Checked;
        }

        private void ChkBandedRow_CheckedChanged(object sender, EventArgs e)
        {
            if (_activeTable != null)
                _activeTable.BandedRows = chkBandedRow.Checked;
        }

        private void ChkBandedColumn_CheckedChanged(object sender, EventArgs e)
        {
            if (_activeTable != null)
                _activeTable.BandedColumns = chkBandedColumn.Checked;
        }

        private void ChkLastColumn_CheckedChanged(object sender, EventArgs e)
        {
            if (_activeTable != null)
                _activeTable.LastColumn = chkLastColumn.Checked;
        }

        private void ChkTotalRow_CheckedChanged(object sender, EventArgs e)
        {
            if (_activeTable != null)
                _activeTable.TotalRowVisible = chkTotalRow.Checked;
        }

        private void ChkFirstColumn_CheckedChanged(object sender, EventArgs e)
        {
            if (_activeTable != null)
                _activeTable.FirstColumn = chkFirstColumn.Checked;
        }

        private void ChkHeaderRow_CheckedChanged(object sender, EventArgs e)
        {
            if (_activeTable != null)
                _activeTable.HeaderRowVisible = chkHeaderRow.Checked;
        }
        #endregion
    }
}


//
//TableStyles.Designer.cs
//


namespace Spread.Table.Features
{
    partial class TableStyles
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.label1 = new System.Windows.Forms.Label();
            this.chkHeaderRow = new System.Windows.Forms.CheckBox();
            this.chkFirstColumn = new System.Windows.Forms.CheckBox();
            this.chkTotalRow = new System.Windows.Forms.CheckBox();
            this.chkBandedRow = new System.Windows.Forms.CheckBox();
            this.chkBandedColumn = new System.Windows.Forms.CheckBox();
            this.chkLastColumn = new System.Windows.Forms.CheckBox();
            this.chkFilterButton = new System.Windows.Forms.CheckBox();
            this.comboStyle = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread);
            this.pnlMain.Size = new System.Drawing.Size(505, 487);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.label2);
            this.pnlControls.Controls.Add(this.comboStyle);
            this.pnlControls.Controls.Add(this.chkFilterButton);
            this.pnlControls.Controls.Add(this.chkLastColumn);
            this.pnlControls.Controls.Add(this.chkBandedColumn);
            this.pnlControls.Controls.Add(this.chkBandedRow);
            this.pnlControls.Controls.Add(this.chkTotalRow);
            this.pnlControls.Controls.Add(this.chkFirstColumn);
            this.pnlControls.Controls.Add(this.chkHeaderRow);
            this.pnlControls.Controls.Add(this.label1);
            this.pnlControls.Size = new System.Drawing.Size(167, 487);
            // 
            // fpSpread
            // 
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread_Sheet1});
            this.fpSpread.Size = new System.Drawing.Size(505, 487);
            this.fpSpread.TabIndex = 0;
            // 
            // fpSpread_Sheet1
            // 
            this.fpSpread_Sheet1.Reset();
            this.fpSpread_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(5, 24);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(141, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Please select the table area:";
            // 
            // chkHeaderRow
            // 
            this.chkHeaderRow.AutoSize = true;
            this.chkHeaderRow.Checked = true;
            this.chkHeaderRow.CheckState = System.Windows.Forms.CheckState.Checked;
            this.chkHeaderRow.Location = new System.Drawing.Point(8, 49);
            this.chkHeaderRow.Name = "chkHeaderRow";
            this.chkHeaderRow.Size = new System.Drawing.Size(86, 17);
            this.chkHeaderRow.TabIndex = 1;
            this.chkHeaderRow.Text = "Header Row";
            this.chkHeaderRow.UseVisualStyleBackColor = true;
            // 
            // chkFirstColumn
            // 
            this.chkFirstColumn.AutoSize = true;
            this.chkFirstColumn.Location = new System.Drawing.Point(8, 72);
            this.chkFirstColumn.Name = "chkFirstColumn";
            this.chkFirstColumn.Size = new System.Drawing.Size(83, 17);
            this.chkFirstColumn.TabIndex = 2;
            this.chkFirstColumn.Text = "First Column";
            this.chkFirstColumn.UseVisualStyleBackColor = true;
            // 
            // chkTotalRow
            // 
            this.chkTotalRow.AutoSize = true;
            this.chkTotalRow.Location = new System.Drawing.Point(8, 95);
            this.chkTotalRow.Name = "chkTotalRow";
            this.chkTotalRow.Size = new System.Drawing.Size(75, 17);
            this.chkTotalRow.TabIndex = 3;
            this.chkTotalRow.Text = "Total Row";
            this.chkTotalRow.UseVisualStyleBackColor = true;
            // 
            // chkBandedRow
            // 
            this.chkBandedRow.AutoSize = true;
            this.chkBandedRow.Checked = true;
            this.chkBandedRow.CheckState = System.Windows.Forms.CheckState.Checked;
            this.chkBandedRow.Location = new System.Drawing.Point(8, 164);
            this.chkBandedRow.Name = "chkBandedRow";
            this.chkBandedRow.Size = new System.Drawing.Size(88, 17);
            this.chkBandedRow.TabIndex = 4;
            this.chkBandedRow.Text = "Banded Row";
            this.chkBandedRow.UseVisualStyleBackColor = true;
            // 
            // chkBandedColumn
            // 
            this.chkBandedColumn.AutoSize = true;
            this.chkBandedColumn.Location = new System.Drawing.Point(8, 141);
            this.chkBandedColumn.Name = "chkBandedColumn";
            this.chkBandedColumn.Size = new System.Drawing.Size(101, 17);
            this.chkBandedColumn.TabIndex = 5;
            this.chkBandedColumn.Text = "Banded Column";
            this.chkBandedColumn.UseVisualStyleBackColor = true;
            // 
            // chkLastColumn
            // 
            this.chkLastColumn.AutoSize = true;
            this.chkLastColumn.Location = new System.Drawing.Point(8, 118);
            this.chkLastColumn.Name = "chkLastColumn";
            this.chkLastColumn.Size = new System.Drawing.Size(84, 17);
            this.chkLastColumn.TabIndex = 6;
            this.chkLastColumn.Text = "Last Column";
            this.chkLastColumn.UseVisualStyleBackColor = true;
            // 
            // chkFilterButton
            // 
            this.chkFilterButton.AutoSize = true;
            this.chkFilterButton.Checked = true;
            this.chkFilterButton.CheckState = System.Windows.Forms.CheckState.Checked;
            this.chkFilterButton.Location = new System.Drawing.Point(8, 187);
            this.chkFilterButton.Name = "chkFilterButton";
            this.chkFilterButton.Size = new System.Drawing.Size(82, 17);
            this.chkFilterButton.TabIndex = 7;
            this.chkFilterButton.Text = "Filter Button";
            this.chkFilterButton.UseVisualStyleBackColor = true;
            // 
            // comboStyle
            // 
            this.comboStyle.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.comboStyle.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboStyle.FormattingEnabled = true;
            this.comboStyle.Location = new System.Drawing.Point(8, 244);
            this.comboStyle.Name = "comboStyle";
            this.comboStyle.Size = new System.Drawing.Size(138, 21);
            this.comboStyle.TabIndex = 1;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(5, 228);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(72, 13);
            this.label2.TabIndex = 8;
            this.label2.Text = "Built-in Styles:";
            // 
            // TableStyles
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "TableStyles";
            this.Size = new System.Drawing.Size(673, 487);
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            this.pnlControls.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread;
        private FarPoint.Win.Spread.SheetView fpSpread_Sheet1;
        private System.Windows.Forms.CheckBox chkLastColumn;
        private System.Windows.Forms.CheckBox chkBandedColumn;
        private System.Windows.Forms.CheckBox chkBandedRow;
        private System.Windows.Forms.CheckBox chkTotalRow;
        private System.Windows.Forms.CheckBox chkFirstColumn;
        private System.Windows.Forms.CheckBox chkHeaderRow;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.CheckBox chkFilterButton;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ComboBox comboStyle;
    }
}
